{
 "cells": [
  {
   "cell_type": "raw",
   "id": "4cebeec0",
   "metadata": {},
   "source": [
    "---\n",
    "sidebar_label: AI21 Labs\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e49f1e0d",
   "metadata": {},
   "source": [
    "# ChatAI21\n",
    "\n",
    "This notebook covers how to get started with AI21 chat models.\n",
    "\n",
    "## Installation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c3bef91",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-15T06:50:44.929635Z",
     "start_time": "2024-02-15T06:50:41.209704Z"
    }
   },
   "outputs": [],
   "source": [
    "!pip install -qU langchain-ai21"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b4f3e15",
   "metadata": {},
   "source": [
    "## Environment Setup\n",
    "\n",
    "We'll need to get a [AI21 API key](https://docs.ai21.com/) and set the `AI21_API_KEY` environment variable:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62e0dbc3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from getpass import getpass\n",
    "\n",
    "os.environ[\"AI21_API_KEY\"] = getpass()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4828829d3da430ce",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "39353473fce5dd2e",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='Bonjour, comment vas-tu?')"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_ai21 import ChatAI21\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "chat = ChatAI21(model=\"j2-ultra\")\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", \"You are a helpful assistant that translates English to French.\"),\n",
    "        (\"human\", \"Translate this sentence from English to French. {english_text}.\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "chain = prompt | chat\n",
    "chain.invoke({\"english_text\": \"Hello, how are you?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c159a79f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
